/**
 *  *2010-6-7 下午03:33:27  add 
 */
package com.base.myproject.client.ui.celleditor;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.FieldEvent;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;

/**
 * 简单的combobox
 * 延迟加载
 * BaseComboBoxX bc16 = new BaseComboBoxX("name","dictionary", "where type='hr_p'");
 * 或者
 * BaseComboBoxX bc16 = new BaseComboBoxX("name","salary","dictionary", "where type='hr_p'");
 * 可以通过设置
 *  String html = "<tpl for=\".\"><div class=\"x-combo-list-item\">--{value}{extdisplayname}</div></tpl>";
 *  	C_num30.setTemplate(html);
 *  显示冗余信息	
 * 内容里不允许有null
 * @author Administrator
 *2010-6-7
 * javazgw@gmail.com
 */
public class BaseComboBoxX  extends ComboBox<BusiObject> {
 
	String column,tablename,filter;

	String extdisplayname; //显示的冗余信息
	boolean b=true;
	protected  BaseComboBoxX() {
	    setDisplayField("value");
	    setStore(new BaseStore<BusiObject>());
		setTriggerAction(TriggerAction.ALL);
		setLazyRender(true);
	  }
	
	public BaseComboBoxX( String column,final String tablename,final String filter)
	{
		this(column,null,tablename,filter);
	
		
	}
	
	public BaseComboBoxX( String column,String extdisplayname,final String tablename,final String filter)
	{
		this();
		this.column = column;
		this.extdisplayname = extdisplayname;
		this.tablename = tablename;
		
		this.filter = filter;
		
	}
	 public BusiObject findModel(Object value) {
		 //如果本身就是BusiObject 那么直接返回。
		 if(value instanceof BusiObject)
		 {
			 return (BusiObject)value;
		 }
		 BusiObject val = null;
		 if(store.getModels().size()>0)
		 {
		    for (BusiObject c : store.getModels()) {
		      if (c.get("value").equals(value)) {
		        val = c;
		        break;
		      }
		    }
		 }
		 else
		 {
			 val = new BusiObject();
			 val.setNotFlag("value", value);
		 }
		    return val;
		  }
	 
		public void doQuery(String q, boolean forceAll) {
			if (q == null) {
				q = "";
			}

			FieldEvent fe = new FieldEvent(this);
			fe.setValue(q);
			if (!fireEvent(Events.BeforeQuery, fe)) {
				return;
			}

			if (forceAll) {

			//	store.getLoader().load(getParams(q));
			

			}
			if(b)
			{
			ModelType combotype = new ModelType();
			combotype.setRoot("records");
			combotype.setRecordName("record");
			combotype.setTotalName("totle");
			 combotype.addField("code");
			combotype.addField("value");
			if(extdisplayname!=null)
			{
				combotype.addField("extdisplayname");
				
			}

			StoreInfo comboinfo = new StoreInfo();
			comboinfo.setTablename("s_service");
			comboinfo.setForcenolimit("yes");
			if(extdisplayname!=null)
			{
				comboinfo
				.setSql("select "+column+" as value,"+column+" as code,"+extdisplayname+" as extdisplayname from "+tablename+" "+filter);
	
				
			}
			else
			{
			comboinfo
					.setSql("select "+column+" as value,"+column+" as code from "+tablename+" "+filter);
			}
			this.store = new BaseStore<BusiObject>(combotype, comboinfo);
			initList();
			
			b=false;
			}
			
			this.store.clearFilters();
			if(q.length()>0)
			{
				this.store.filter(getDisplayField(), q);
			}
			expand();
		}
		
		public CellEditor getCellEditor() {
			CellEditor editor = new CellEditor(this) {
				@Override
				public Object preProcessValue(Object value) {
					if (value == null) {
						return value;
					}
					return ((BaseComboBoxX)this.getField()).findModel(value.toString());
				}

				@Override
				public Object postProcessValue(Object value) {
					if (value == null) {
						return value;
					}
					return ((ModelData) value).get("value");
				}
			};
			return editor;
		}

		public String getFilter() {
			return filter;
		}

		public void setFilter(String filter) {
			b=true; //需要重载数据
			this.filter = filter;
		}
		
		
}
